<?php

class PhanQuyenController extends BaseController {

    /**
     * Display a listing of the resource.
     * GET /phanquyen
     *
     * @return Response
     */
    public function postDsquyen() {
        $id_tai_khoan = Input::get('id');
        $results = DB::table('chuc_nangs')->get();
        $html = '';
        foreach ($results as $results) {
            $check_quyen = DB::table('chuc_nang_has_tai_khoans')
                    ->where('ChucNang_Id', $results->id)
                    ->where('TaiKhoan_Id', $id_tai_khoan)
                    ->count();
            $html .= '<div class="form-group">';
            $html .= '<div class="checkbox">';
            $html .= '<label>';
            $html .= '<input type="checkbox"';
            if ($check_quyen == '1') {
                $html .= 'checked ';
            }
            $html .= 'onclick="set_permission(' . $id_tai_khoan . ',' . $results->id . ');" id="' . $results->id . '"/>';
            $html .= $results->ten_chuc_nang;
            $html .= '</label>';
            $html .= '</div></div>';
        }
        return $html;
    }

    public function postSetpermission() {
        $id_tai_khoan = Input::get('id_tai_khoan');
        $id_chuc_nang = Input::get('id_chuc_nang');
        $check_quyen = DB::table('chuc_nang_has_tai_khoans')
                ->where('ChucNang_Id', $id_chuc_nang)
                ->where('TaiKhoan_Id', $id_tai_khoan)
                ->count();
        if ($check_quyen == '1') {
            $check_quyen = DB::table('chuc_nang_has_tai_khoans')
                    ->where('ChucNang_Id', $id_chuc_nang)
                    ->where('TaiKhoan_Id', $id_tai_khoan)
                    ->delete();
            return 0;
        } else {
            $id = DB::table('chuc_nang_has_tai_khoans')
                    ->insertGetId(array('ChucNang_Id' => $id_chuc_nang, 'TaiKhoan_Id' => $id_tai_khoan));
            return $id;
        }
    }

}
